<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="resource/css/bootstrap.css">
    <script src="resource/js/jquery-3.2.1.js"></script>
    <script src="resource/js/bootstrap.js"></script>
    <script type="text/javascript" src="/resource/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
    <h2>SPU管理</h2>
    <hr>
    <form:form modelAttribute="spu" action="add" enctype="multipart/form-data" method="post">
        <table class="table">
           <tr>
               <td>商品名称:</td>
               <td><form:input path="name"></form:input></td>
           </tr>
            <tr>
                <td>标题:</td>
                <td><form:input path="title"></form:input></td>
            </tr>
            <tr>
                <td>分类</td>
                <td>
                    <select name="tid1" onchange="t1()"></select>
                    <select name="tid2" onchange="t2()"></select>
                    <select name="tid3" ></select>
                </td>
            </tr>
            <tr>
                <td>是否上架:</td>
                <td>
                    <input type="radio" name="status" value="1">上架
                    <input type="radio" name="status" value="0">下架
                </td>
            </tr>
            <tr>
                <td>品牌:</td>
                <td>
                    <select name="pid">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="file"  onchange="upFile(this)">
                </td>
                <td><span id="sp1">

                </span></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form:form>
<script>
    var tid1=0;
    var tid2=0;
    var tid3=0;
    function upFile(obj){
        var formdata=new FormData();
        formdata.append("file",obj.files[0]);
        $.ajax({
            type:"post",
            data:formdata,
            url:"getpicurl",
            contentType:false,
            processData:false,
            success:function(msg){
                $("#sp1").html('')
                $("#sp1").append("<img src='"+msg+"' width='80px' height='120px'>");
                $("#sp1").append("<input type='hidden' name='picurl' value='"+msg+"'>");
            }
        })
    }
    $.post("selectBypid",function(msg){
        var str="<option value=''>请选择</option>"
        for(var i in msg){
            str+="<option value='"+msg[i].pid+"'>"+msg[i].pname+"</option>";
        }
        $("[name='id']").html(str);
    })
    //获取品牌
    $.post("selectBypid",function(msg){
        var str="<option value=''>请选择</option>";
        for(var i in msg){
            str+="<option value='"+msg[i].id+"'>"+msg[i].pname+"</option>";
        }
        $("[name='pid']").html(str)
    })

    //获取第一个下拉
    $.post("selectTid",{tid1:tid1},function(msg){
        var str="<option value=''>请选择</option>";
        for(var i in msg){
            str+="<option value='"+msg[i].id+"'>"+msg[i].tname+"</option>";
        }
        $("[name='tid1']").html(str);
    })
    //tid时间
    function t1(){
         tid2=$("[name='tid1'] :selected").val();
        $.post("selectTid",{tid1:tid2},function(msg){
            var str="<option value=''>请选择</option>";
            for(var i in msg){
                str+="<option value='"+msg[i].id+"'>"+msg[i].tname+"</option>";
            }
            $("[name='tid2']").html(str)
        })
        if(tid1==''||tid3!=tid2){
            $("[name='tid3']").html("<option value=''>请选择</option>")
        }
    }
    function t2(){
         tid3=$("[name='tid2'] :selected").val();
        $.post("selectTid",{tid1:tid3},function(msg){
            var str="<option value=''>请选择</option>";
            for(var i in msg){
                str+="<option value='"+msg[i].id+"'>"+msg[i].tname+"</option>";
            }
            $("[name='tid3']").html(str)
        })
        if(tid1==''||tid3!=tid2){
            $("[name='tid3']").html("<option value=''>请选择</option>")
        }
    }
</script>
</body>


</html>